# -*- coding: latin-1 -*-
# Copyright (c) 2007-2009 Jorge Ortiz Romo <jortizromo@gmail.com>
#                         Freymam Vallejo Cuero <freturn@gmail.com>
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# Import the module and instantiate a pygraph object

'''
Created on Dec 7, 2009

@author: Freymam Vallejo
@author: Jorge Ortiz
'''

from pygraph.algorithms.minmax import shortest_path

def digraph_find_all_paths(digraph, start, end, path=[]):
    """
    Find all paths from start to end

    @type graph: digraph.
    @param graph: DiGraph.

    @type start: node
    @param start: initial node on the path

    @type end: node
    @param end: finish node on the path

    @type path: list
    @param path: used in the internal process.

    @rtype: list.
    @return: list with all paths from start to end. A path is a node sequence.
    """

    
    path = path + [start]
    
    if start == end:
        return [path]
    if not digraph.has_node(start):
        return []
    paths = []
    for node in digraph.neighbors(start):
        if node not in path:
            newpaths = digraph_find_all_paths(digraph, node, end, path)
            for newpath in newpaths:
                paths.append(newpath)
    return paths

def digraph_find_all_semipaths(digraph, start, end, path=[]):
    """
    Find all semipaths and paths from start to end

    @type graph: digraph.
    @param graph: DiGraph.

    @type start: node
    @param start: initial node on the path

    @type end: node
    @param end: finish node on the path

    @type path: list
    @param path: 

    @rtype: list.
    @return: list with all semipaths and paths from start to end. A path is a node sequence.
    """
    
    path = path + [start]
    
    if start == end:
        return [path]
    if not digraph.has_node(start):
        return []
    paths = []
    for node in digraph.neighbors(start) + digraph.incidents(start):
        if node not in path:
            newpaths = digraph_find_all_semipaths(digraph, node, end, path)
            for newpath in newpaths:
                paths.append(newpath)
    return paths


def digraph_path_length(digraph, ni, nj):
    """
    It computes length of the shortest path between two nodes. 

    @type graph: digraph.
    @param graph: DiGraph.

    @type ni: start node
    @param ni: initial node on the path

    @type nj: node
    @param nj: finish node on the path

    @rtype: number.
    @return: legth of the shortest path.
    """
    
    distances = shortest_path(digraph, ni)
    lengths = distances[1]
    
    return lengths.get(nj)
    
    
def digraph_path_distance(digraph, path):
    """
    It computes length of a path. 

    @type graph: digraph.
    @param graph: DiGraph.

    @type path: list.
    @param path: Path formed by sequence of nodes.

    @rtype: number.
    @return: legth of a path.
    """
    
    return len(path) - 1


def digraph_shortest_path_distance(digraph, ni, nj):
    """
    It computes length of the shortest path between two nodes. 

    @type graph: digraph.
    @param graph: DiGraph.

    @type ni: start node
    @param ni: initial node on the path

    @type nj: node
    @param nj: finish node on the path

    @rtype: number.
    @return: legth of the shortest path.
    """
    
    path = []
    paths = digraph_find_all_paths(digraph, ni, nj, path)
    distance = 0
    distances = []
    
    
    for path in paths:
        distances.append( digraph_path_distance(digraph, path) )

    
    if len(distances) != 0:
        distance = min(distances)
        
    return distance
    

def influence_domain (digraph, ni):
    """
    It finds of influence domain, that is finds all nodes have any path to ni node. 

    @type graph: digraph.
    @param graph: DiGraph.

    @type ni: node.
    @param ni: Node.

    @rtype: list.
    @return: list of nodes have any path to ni node.
    """
    
    influence = []
    
    for node in digraph.nodes():
        if node != ni:
            path = digraph_find_all_paths(digraph, node, ni)
            if len(path) > 0:
                influence.append(node)
    
    return influence

